package logic

import (
	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"
	"2210a/commod/models/mysql"
	"2210a/commod/models/redisinit"
	"context"
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopListLogic) ShopList(in *commodrpc.ShopListRequest) (*commodrpc.ShopListResponse, error) {
	// todo: add your logic here and delete this line
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	key := "ShopList" + strconv.Itoa(int(offset))
	var product mysql.Products
	mysql.Db.Debug().Where("user_id = ?", in.UserId).Find(&product)
	if product.UserId != in.UserId {
		return nil, fmt.Errorf("用户不存在")
	}
	var products []mysql.Products
	get := redisinit.ClientRedis.Get(key)

	if get.Val() == "" {
		query := mysql.Db.Debug().Offset(int(offset)).Limit(int(pageSize)).Find(&products)
		query.Where("show_name = LIKE", "%"+in.ShopName+"%").
			Order("price desc").Order("created_at desc").Find(&products)
		marshal, _ := json.Marshal(&products)
		//set :=
		redisinit.ClientRedis.Set(key, marshal, time.Hour*24)
		//for _, v := range set.Val() {
		//
		//}
	} else {
		json.Unmarshal([]byte(get.Val()), &products)
	}
	var slice []*commodrpc.ShopList
	for _, v := range products {
		list := commodrpc.ShopList{
			Id:             int64(v.ID),
			UserId:         v.UserId,
			Avatar:         v.Avatar,
			ShopName:       v.ShopName,
			Price:          v.Price,
			Gender:         v.Gender,
			ShopNavigation: v.ShopNavigation,
			Address:        v.Address,
			Quantity:       v.Quantity,
			Introduce:      v.Introduce,
			Status:         v.Status,
		}
		slice = append(slice, &list)
	}
	return &commodrpc.ShopListResponse{List: slice}, nil
}

//{
//	"code": 200,
//	"message": "商品排行成功",
//	"data": [
//		{
//			"id": 8,
//			"userId": 1,
//			"avatar": "额大润发韬光养晦",
//			"shopName": "见客户苟富贵1",
//			"price": 931,
//			"gender": "男",
//			"shopNavigation": "科技股份都是",
//			"address": "空间医疗费",
//			"quantity": 5368,
//			"introduce": "无色大润发深层次",
//			"status": "未上架",
//			"likes": 12
//		},
//		{
//			"id": 6,
//			"userId": 1,
//			"avatar": "代扣代缴快点快点",
//			"shopName": "科技好家伙",
//			"price": 856,
//			"gender": "女",
//			"shopNavigation": "打底裤计划",
//			"address": "很高的天沟",
//			"quantity": 8576,
//			"introduce": "看看极恶都市",
//			"status": "已上架",
//			"likes": 9
//		},
//		{
//			"id": 7,
//			"userId": 1,
//			"avatar": "额大润发韬光养晦",
//			"shopName": "见客户苟富贵1",
//			"price": 931,
//			"gender": "男",
//			"shopNavigation": "科技股份都是",
//			"address": "空间医疗费",
//			"quantity": 5368,
//			"introduce": "无色大润发深层次",
//			"status": "未上架",
//			"likes": 8
//		},
//		{
//			"id": 5,
//			"userId": 1,
//			"avatar": "http://spc3wgrgk.hn-bkt.clouddn.com/20250102141917.png",
//			"shopName": "让人口结构",
//			"price": 384,
//			"gender": "女",
//			"shopNavigation": "打底裤计划",
//			"address": "很高的呃呃付",
//			"quantity": 2345,
//			"introduce": "付款时间都觉得大口大口的 ",
//			"status": "已上架",
//			"likes": 5
//		},
//		{
//			"id": 2,
//			"userId": 1,
//			"avatar": "http://spc3wgrgk.hn-bkt.clouddn.com/20250102141917.png",
//			"shopName": "让人口结构",
//			"price": 384,
//			"gender": "男",
//			"shopNavigation": "京沪高铁",
//			"address": "很高的呃呃付",
//			"quantity": 2345,
//			"introduce": "付款时间都觉得大口大口的 ",
//			"status": "已上架",
//			"likes": 5
//		},
//		{
//			"id": 3,
//			"userId": 1,
//			"avatar": "http://spc3wgrgk.hn-bkt.clouddn.com/20250102141917.png",
//			"shopName": "让人口结构",
//			"price": 384,
//			"gender": "女",
//			"shopNavigation": "看见担任",
//			"address": "很高的呃呃付",
//			"quantity": 2345,
//			"introduce": "付款时间都觉得大口大口的 ",
//			"status": "未上架",
//			"likes": 4
//		}
//	]
//}
